#include "searcher.hpp"
#include "cpp-httplibs/httplib.h"
#include <iostream>
#include <string>
// #include <jsoncpp/json>

const std::string input = "raw.txt";
const std::string root_path = "./wwwroot";

int main()
{
    ns_searcher::Searcher searcher;
    std::cout << "创建对象成功" << std::endl;
    searcher.InitSearcher(input);
    std::cout << "创建索引成功成功" << std::endl;

    httplib::Server svr;
    svr.set_base_dir(root_path.c_str());
    svr.Get("/s", [&searcher](const httplib::Request &req, httplib::Response &rsp)
            {
                if (!req.has_param("word"))
                {
                    rsp.set_content("必须要有搜索关键字", "text/plain; charset=utf-8");
                    return;
                }
                std::string word = req.get_param_value("word");
                std::cout << "用户搜索的：" << word << std::endl;
                std::string json_string;
                searcher.Search(word, &json_string);
                rsp.set_content(json_string, "application/json");
                // rsp.set_content("你好,世界!", "text/plain; charset=utf-8");
            });
    std::cout << "服务器启动成功" << std::endl;
    svr.listen("0.0.0.0", 8080);
    return 0;

}